home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
007
/
crc.lqr
/
CRC.DOC
< prev
next >
Wrap
Text File
|
1985-06-03
|
7KB
|
136 lines
Cycliπ Redundanc∙ Checδ (CRC⌐ Functions
John F. Ratti
c/o Computer Power, Inc.
P. O. 2388
Jacksonville, FL 32231
(904) 350-1400
Thi≤á i≤á thσ .DO├ filσ fo≥ botΦ CRC.├á anΣá CRCCALC.ASM«á Thesσá function≤
providσáá CCIT╘áá o≥áá IB═áá stylσáá CR├áá calculations«áá CRC.├áá contain≤
Lattice/Microsof⌠á "Cóá function≤á t∩ initializσ anΣ terminatσá CR├á calcu-
lations¼ anΣ t∩ perforφ CR├ calculation≤ oε onσ bytσ a⌠ ß time« CRCCALC.AS═
i≤á aε assembl∙ languagσ functioε whicΦ calculate≤ thσ CR├ fo≥ ß numbe≥á oµ
byte≤á oµá datß (u≡ t∩ 64╦ byte≤ a⌠ ß time⌐ severa∞ time≤ faste≥á thaεá thσ
byte-at-a-timσ functioε (CRC_UPDATE⌐ iε CRC.C«á ┴ shor⌠ descriptioε oµá thσ
function≤ follows.
CRC_CLEA╥á clear≤ thσ CR├ t∩ zero«á Thi≤ functioε shoulΣ bσ calleΣ prio≥ t∩
thσ star⌠ oµ processinτ fo≥ ß blocδ oµ data.
CRC_UPDAT┼ adjust≤ thσ CR├ fo≥ ß character« I⌠ mus⌠ bσ calleΣ oncσ fo≥ eacΦ
characte≥ t∩ bσ processed iε ß blocδ oµ data.
CRCCAL├á adjust≤á thσ CR├ fo≥ ß numbe≥ oµ byte≤ oµ datßá a⌠á ßá time«á Thi≤
functioεá i≤á writteε iε assembler¼á anΣ i≤ faste≥ thaε repeateΣá call≤á t∩
CRC_UPDATE« Thσ callinτ sequencσ fo≥ thi≤ functioε i≤ provideΣ below.
CRC_FINIS╚á terminate≤ thσ CR├ processinτ oµ ß blocδ oµ data«á I⌠ i≤á useΣ
onl∙á wheε originall∙ calculatinτ thσ CR├ oµ ß block¼á anΣ i≤ calleΣá afte≥
al∞ byte≤ havσ beeε processed« Wheε re-calculatinτ (checking⌐ thσ CR├ fo≥ ß
blocδ oµ data¼á al∞ oµ thσ datß anΣ botΦ byte≤ oµ thσ CR├ mus⌠ bσ processeΣ
b∙ CRC_UPDAT┼ o≥ CRCCALC«á Iµ thσ resultinτ CR├ i≤ zero¼á theε thσ blocδ i≤
good.
A discussion of CRCCALC:
CRCCAL├á calculate≤á thσá CR├ fo≥ ß numbe≥ oµ byte≤ oµá datßá a⌠á once«á I⌠
return≤á aε updateΣ CR├ afte≥ processinτ al∞ thσ bytes«á Thi≤á functioεá i≤
usuall∙á calleΣá onl∙ oncσ fo≥ eacΦ blocδ oµ datß oε whicΦ ß CR├á i≤á beinτ
calculated«á However¼ i⌠ caε bσ calleΣ repeatedly¼ iµ neeΣ be¼ t∩ calculatσ
thσ CR├ wherσ multiplσ record≤ (o≥ othe≥ group≤ oµ data⌐ arσ t∩ bσ includeΣ
iεá ßá singlσ CRC«á Fo≥ example¼á wσ calculatσ thσ CR├ oµ file≤á whicΦá arσ
severa∞á megabyte≤ lonτ b∙ readinτ 32╦ segment≤ oµ thσ filσ anΣá processinτ
eacΦ witΦ CRCCALC« Oncσ thσ entirσ filσ ha≤ beeε processed¼ theε CRC_FINIS╚
i≤á calleΣá t∩ terminatσ thσ processinτ anΣ producσ onσ 16-bi⌠ CR├ fo≥á thσ
entirσ file«á Fastes⌠ speed≤ wil∞ bσ attaineΣ b∙ processinτ a≤ mucΦ datß iε
eacΦ cal∞ t∩ CRCCAL├ a≤ possible.
CRCCALC.AS═á i≤ configureΣ fo≥ thσ Lattice/Microsof⌠ "Có compile≥ "L-modeló
(3▓á bi⌠á codσ anΣ datß addresses⌐ callinτ sequence«á Thi≤á caεá bσá easil∙
converteΣá t∩á othe≥á memor∙ model≤ o≥ fo≥ thσ callinτ sequence≤á oµá othe≥
compilers, since only the call/return sequences will differ.
Thσ callinτ sequencσ fo≥ eacΦ oµ thσ "Có languagσ function≤ i≤ noteΣ iε thσ
comment≤á fo≥ tha⌠ function«á Sincσ thσ callinτ sequencσ fo≥ CRCCAL├ i≤ no⌠
as obvious, a discussion of that calling sequence follows.
Calling sequence for CRCCALC:
crc = crccalc(crc, ptr, length);
short crc; /* 16-bit CRC being calculated */
char *ptr; /* 32-bit pointer to data to be processed */
unsigneΣálength╗á /* 16-bit lengtΦ oµ datß (▒ to 65535 bytes) */
Notσá tha⌠ thσ valuσ iε thσ variablσ "crcó i≤ passeΣ t∩ CRCCALC¼á anΣá tha⌠
thσ updated value is returned and stored back in the same variable.
Example using CRCCALC:
short crc; /* 16-bit CRC being calculated */
char *ptr; /* 32-bit pointer to data to be processed */
unsigneΣálength╗á /* 16-bit lengtΦ oµ datß (▒ to 65535 bytes) */
char data[10000]; /* data to be processed */
crc = crc_clear(); /* reset crc for next block of data */
gets(data); /* get block of data to be processed */
length = strlen(data); /* determine the length of the data */
ptr = &data[0]; /* point to the first byte of the data */
crc = crccalc(crc, ptr, length); /* calculate the CRC for this block */
crc = crc_finish(crc); /* terminate crc processing for this block */
/* At this point the variable "crc" contains the crc for the string in the */
/* variable "data". Now we will append the crc to the data, high-order byte */
/* first¼ theε lo≈ orde≥ byte« */
data[length▌ ╜ (char⌐ ((crπ ª 0xff00⌐ >╛ 8)╗ /¬ appenΣ crπ high-orde≥ bytσ */
data[lengtΦ ½ 1▌ ╜ (char⌐ (crπ ª 0x00ff)╗ /¬ appenΣ crπ low-orde≥ bytσ */
/¬ Thσ variablσ "dataó no≈ contain≤ thσ origina∞ strinτ followeΣ b∙ thσ CRC.*/
/¬ Notσ tha⌠ eithe≥ bytσ oµ thσ CR├ coulΣ bσ zero¼ s∩ "dataó caε n∩ longe≥ */
/* be treated as a string. */
Example using CRC_UPDATE:
short crc; /* 16-bit CRC being calculated */
unsigneΣálength╗á /* 16-bit lengtΦ oµ datß (▒ to 65535 bytes) */
char data[10000]; /* data to be processed */
int i; /* index for data */
crc = crc_clear(); /* reset crc for next block of data */
gets(data); /* get block of data to be processed */
length = strlen(data); /* determine the length of the data */
for (i = 0; i < length; i++)
{ /* for each byte of the string in "data" */
crπ ╜ crc_update(crc¼á data[i])╗ /¬ calculatσ thσ CR├ fo≥ thi≤ bytσ */
} /* end of for loop */
crc = crc_finish(crc); /* terminate crc processing for this block */
/* At this point the variable "crc" contains the crc for the string in the */
/* variable "data". Now we will append the crc to the data, high-order byte */
/* first¼ theε lo≈ orde≥ byte« */
data[length▌ ╜ (char⌐ ((crπ ª 0xff00⌐ >╛ 8)╗ /¬ appenΣ crπ high-orde≥ bytσ */
data[lengtΦ ½ 1▌ ╜ (char⌐ (crπ ª 0x00ff)╗ /¬ appenΣ crπ low-orde≥ bytσ */
/¬ Thσ variablσ "dataó no≈ contain≤ thσ origina∞ strinτ followeΣ b∙ thσ CRC.*/
/¬ Notσ tha⌠ eithe≥ bytσ oµ thσ CR├ coulΣ bσ zero¼ s∩ "dataó caε n∩ longe≥ */
/* be treated as a string. */
/***************************** end of CRC.DOC ********************************/